<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title></title>
</head>
<body>
	<?php
	
	session_start();
	if(!$_SESSION['IsLogin'] || $_SESSION['IsLogin']!='true'){
		echo "<script>alert('登录超时或未登录，请先登录！');location.href='./Login.php';</script>";
	}
			
	// 引入数据库连接
	include "db.php";
	function alert($str){
		echo "<script>alert('$str');</script>";
	}
	$id = $_REQUEST["id"]??"";
	$cmd = $_REQUEST["cmd"]??"";
	$title='';
	$author='';$content='';$isActived='';$remark='';$photo='';
	if($cmd == ''){
		// 首次加载
		$data = mysqli_query($conn,"select * from Articles where ArticleId = $id");
		$row = mysqli_fetch_array($data);
		if($row == NULL){
			alert('获取数据失败');
		}else{
			$title = $row["Title"]??"";
			$author = $row["Author"]??"";
			$content = $row["Content"]??"";
			$isActived = $row["IsActived"]??"";
			$remark = $row["Remark"]??"";
			$photo = $row["Photo"]??"";
		}
	}else{
		// 提交
		$title = $_REQUEST["title"]??"";
		$author = $_REQUEST["author"]??"";
		$content = $_REQUEST["content"]??"";
		$isActived = $_REQUEST["isActived"]??"";
		$remark = $_REQUEST["remark"]??"";
		$photo = $_REQUEST['oldPhoto']??"";
		// 判断是否是提交
		if($cmd==''){
			return;
		}
		$valid = true;
		if(strlen(trim($title)) == 0){
			$valid = false;
			alert('请输入标题');
		}
		if(strlen(trim($author))==0){
			$valid = false;
			alert('请输入作者');
		}
		if(strlen(trim($content))==0){
			$valid = false;
			alert('请输入内容');
		}
		if(strlen(trim($isActived))==0){
			$valid = false;
			alert('请选择是否启用');
		}
		$fileName = $_REQUEST['oldPhoto']??"";
		if($_FILES["photo"]["size"] > 0){
			// 保存
			$fileName = time().".png";
			move_uploaded_file($_FILES["photo"]["tmp_name"],"D:\\nginx-1.18.0\\html\\exam\\images\\".$fileName);
		}
		
		
		if($valid){
			// 验证通过后，进行保存
			$sql = "update Articles set title='$title',content='$content',author='$author',isActived=$isActived,updateDate=NOW(),remark='$remark',photo='$fileName' where articleId=$id";
			$result = mysqli_query($conn,$sql);
			if($result){
				alert('添加成功');
				echo "<script>location.href='./Index.php';</script>";
			}else{
				alert('添加失败');
			}
		}
	}
		
	?>
	
	<!-- 上传文件，form有要求， enctype="multipart/form-data" method="post"-->
	<form enctype="multipart/form-data" method="post">
		<input type="hidden" name='cmd' value='add'/>
		<input type='hidden' name='oldPhoto' value='<?php echo $photo;?>'>
		<img width='80px' src='./images/<?php echo $photo;?>'>
		<br/>
		封面：<input type="file" name="photo"/>
		<br/>
		标题：<input type="text" name='title' value='<?php echo $title;?>'/>
		<br/>
		作者：<input type="text" name='author' value='<?php echo $author;?>'/>
		<br />
		内容：<input type="text" name='content' value='<?php echo $content;?>'/>
		<br/>
		是否启用：
		<?php
			if($isActived=='1'){
				echo <<<shi
					<input type='radio' name='isActived' id='abc' value='1' checked='checked'/> 
					<label for='abc'>启用</label>
					<input type='radio' name='isActived' id='hjk' value='0'/> 
					<label for='hjk'>禁用</label>
shi;
			}else if($isActived =='0'){
				echo <<<shi
					<input type='radio' name='isActived' id='abc' value='1'/> 
					<label for='abc'>启用</label>
					<input type='radio' name='isActived' id='hjk' value='0'  checked='checked'/> 
					<label for='hjk'>禁用</label>
shi;
			}else{
				echo <<<shi
					<input type='radio' name='isActived' id='abc' value='1'/> 
					<label for='abc'>启用</label>
					<input type='radio' name='isActived' id='hjk' value='0'/> 
					<label for='hjk'>禁用</label>
shi;
			}
		?>
		
		<br/>
		备注：<input type="text" name="remark" value='<?php echo $remark;?>'/>
		<br/>
		<input type="submit" value="保存"/>
	</form>

</body>
</html>	